//
//  RegistrationAvatarCell.swift
//  PorscheRegistration
//
//  Created by Jinzhu Liu on 2023/11/16.
//

import UIKit
import Combine

class RegistrationAvatarCell: RegistrationBaseCell {
    
    var containerView: UIView
    var avatarImageView: UIImageView
    var photoPickButton: UIButton
    
    private var cancelableSet = Set<AnyCancellable>()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        self.containerView = UIView()
        self.avatarImageView = UIImageView()
        self.photoPickButton = UIButton(type: .custom)
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    override func setupUI() {
        super.setupUI()
        self.contentView.addSubview(containerView)
        containerView.translatesAutoresizingMaskIntoConstraints = false
        
        avatarImageView.layer.cornerRadius = 30.0
        avatarImageView.layer.borderWidth = 5.0
        avatarImageView.layer.borderColor = UIColor.white.cgColor
        avatarImageView.clipsToBounds = true
        
        photoPickButton.backgroundColor = .clear
        photoPickButton.addTarget(self, action: #selector(buttonTouched), for: .touchUpInside)
        
        containerView.addSubview(avatarImageView)
        containerView.addSubview(photoPickButton)
        
        avatarImageView.translatesAutoresizingMaskIntoConstraints = false
        photoPickButton.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            containerView.heightAnchor.constraint(greaterThanOrEqualToConstant: 60.0),
            containerView.widthAnchor.constraint(equalTo: containerView.heightAnchor),
            containerView.trailingAnchor.constraint(equalTo: self.contentView.safeAreaLayoutGuide.trailingAnchor, constant: -16.0),
            containerView.topAnchor.constraint(equalTo: self.contentView.safeAreaLayoutGuide.topAnchor, constant: 10.0),
            containerView.bottomAnchor.constraint(equalTo: self.contentView.safeAreaLayoutGuide.bottomAnchor, constant: -10.0),
            
            avatarImageView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            avatarImageView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
            avatarImageView.topAnchor.constraint(equalTo: containerView.topAnchor),
            avatarImageView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
            
            photoPickButton.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            photoPickButton.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
            photoPickButton.topAnchor.constraint(equalTo: containerView.topAnchor),
            photoPickButton.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
        ])
        
    }
    
    override func update(with viewModel: AnyObject?) {
        super.update(with: viewModel)
        guard let avatarViewModel = viewModel as? RegistrationAvatarCellViewModelType else {
            return
        }
        
        avatarViewModel.output.updateImagePublisher
            .sink { [unowned self] image in
                self.avatarImageView.image = image
            }.store(in: &cancelableSet)
        avatarViewModel.output.updateColorPublisher
            .sink { [unowned self] color in
                self.avatarImageView.layer.borderColor = color.cgColor
            }.store(in: &cancelableSet)
    }
    
    @objc private func buttonTouched(sender: AnyObject) {
        guard let avatarViewModel = viewModel as? RegistrationAvatarCellViewModelType else {
            return
        }
        avatarViewModel.input.showPhotoPicker()
    }
}
